using System;
using CStock;
using CUtil;
using System.Diagnostics;
namespace CStock
{
	
	//[System.Xml.Serialization.XmlRootAttribute("TradeCondition", Namespace="", IsNullable=false)]
    public class TradeEconomyFactory : Factory, ITradeEconomyFactory
    {

        ITradeEconomy tradeEconomy;
       

        public TradeEconomyFactory()
        {
            tradeEconomy = new TradeEconomy();
            this.stockObjectBase = tradeEconomy as StockObjectBase;
        }
      


        #region implementations


     
        public override bool Create_Default()
        {
            string id = "CentralBank";
            //DeleteOldOne();
            this.tradeEconomy.ID = id;

            base.Create_Default();
              
            this.tradeEconomy.Money_Initial = this.tradeEconomy.Money_Current;

         

            //-------------------------------------------------
            //profile
            TradeEconomyProfileFactory tpf = new TradeEconomyProfileFactory();
            tpf.Create_Default();
            this.tradeEconomy.TradeEconomyProfile = (ITradeEconomyProfile)(tpf.StockObjectBase);

            //-------------------------------------------------
            //traders
            TradersFactory tf = new TradersFactory();
            tf.Create_Default();
            this.tradeEconomy.Traders = tf.StockObjectBase as ITraders;

            //....................................


            //System.Collections.ArrayList moneyCollection = CreateMoneyCollection(this.tradeEconomy.Money_Initial, this.tradeEconomy.TradeEconomyProfile.Number_Shares);

            //-------------------------------------------------
            //Portfolios
            PortfoliosFactory pf = new PortfoliosFactory();
            pf.Create_Default();
            pf.Portfolios.Create_Default();

            this.tradeEconomy.Portfolios = pf.Portfolios;
            this.tradeEconomy.Portfolio_Selected = this.tradeEconomy.Portfolios[0];

            //Economy Status
            this.tradeEconomy.EconomyStatusCollection = new EconomyStatusCollection();
            this.tradeEconomy.EconomyStatusCollection.CreateDefault();



            //Debug.Assert(false, "Not implemented");
            return true;



        }
        public override bool Create(string id)
        {
            ITradeEconomy tradeEconomy = this.stockObjectBase as TradeEconomy;

            tradeEconomy.ID = id;
            return Create_TestData();


        }
        public override bool Create_TestData()
        {
            base.Create_TestData();
            this.tradeEconomy.Money_Current = 1000000;
           
            this.tradeEconomy.Money_Initial = this.tradeEconomy.Money_Current;
            int numberOfShareValues = 200;
            
            //-------------------------------------------------
            //profile
            TradeEconomyProfileFactory tpf = new TradeEconomyProfileFactory();
            tpf.Create_TestData(this.tradeEconomy.ID);
            this.tradeEconomy.TradeEconomyProfile = (ITradeEconomyProfile)(tpf.StockObjectBase);

            //-------------------------------------------------
            //traders
            TradersFactory tf = new TradersFactory();
            tf.Create_TestData(this.tradeEconomy.ID);
            this.tradeEconomy.Traders = tf.StockObjectBase as ITraders;
            
            //....................................


            //System.Collections.ArrayList moneyCollection = CreateMoneyCollection(this.tradeEconomy.Money_Initial, this.tradeEconomy.TradeEconomyProfile.Number_Shares);

            //-------------------------------------------------
            //Portfolios
            PortfoliosFactory pf = new PortfoliosFactory();
            pf.Create_TestData();
            pf.Portfolios.Create_TestData(this.tradeEconomy.Money_Initial, this.tradeEconomy.TradeEconomyProfile.Number_Shares, numberOfShareValues);

            this.tradeEconomy.Portfolios = pf.Portfolios;
            this.tradeEconomy.Portfolio_Selected = this.tradeEconomy.Portfolios[0];


            //-------------------------------------------------
            //Creates Portfolios with only one share ! 

            

            //Debug.Assert(false, "Not implemented");
            return true;



        }
       
      
        public System.Collections.ArrayList CreateMoneyCollection(double money, int numberOfItems)
        {
            System.Collections.ArrayList moneyCollection = new System.Collections.ArrayList();

            double moneyPerItem = money / numberOfItems;
            double randomMoney = 0;

            double moneyLeft = money;
            for (int i = 0; i < numberOfItems; i++)
            {
                randomMoney = Convert.ToDouble(RandomGenerator.Random(1, Convert.ToInt32(moneyPerItem * 2)));
                moneyLeft -= randomMoney;
                moneyCollection.Add(randomMoney);
                Debug.WriteLine(i.ToString() + "  Random money = " + randomMoney.ToString()); 
                
            }
            //could be that we spent too much money ...
            if (moneyLeft < 0)
            {
                //get the highest values
                moneyLeft = -moneyLeft;
                
                int iItems = -  1;
                //see, how many items have too much money
                for (int i = 0; i < numberOfItems; i++)
                {
                    double d = (double)moneyCollection[i] ;
                    if (d > moneyPerItem)
                    {
                        iItems++;
                    }
                }
                //now substract the money from them ...
                double moneyRecalc = moneyLeft / iItems;
                for (int i = 0; i < iItems; i++)
                {
                    double d = (double)moneyCollection[i] ;
                    if (d > moneyPerItem)
                    {
                        d -= moneyRecalc;
                        moneyCollection[i] = d;
                    }
                    
                    
                }

            }
            //could be that we spent too much money ...
            if (moneyLeft < 0)
            {
                //get the highest values
                moneyLeft = -moneyLeft;
                
                int iItems = -  1;
                //see, how many items have too much money
                for (int i = 0; i < numberOfItems; i++)
                {
                    double d = (double)moneyCollection[i] ;
                    if (d > moneyPerItem)
                    {
                        iItems++;
                    }
                }
                //now substract the money from them ...
                double moneyRecalc = moneyLeft / iItems;
                for (int i = 0; i < iItems; i++)
                {
                    double d = (double)moneyCollection[i] ;
                    if (d > moneyPerItem)
                    {
                        d -= moneyRecalc;
                        moneyCollection[i] = d;
                    }
                    
                    
                }

            }
            //test:
            moneyLeft = 0;
            for (int i = 0; i < numberOfItems; i++)
            {
                moneyLeft += (double)moneyCollection[i];
              
            }
            
            Debug.WriteLine("  Total money = " + moneyLeft.ToString() + "Compare to : " + this.tradeEconomy.Money_Initial.ToString());
            moneyLeft -= this.tradeEconomy.Money_Initial;
            Debug.WriteLine("  Difference = " + moneyLeft.ToString() ); 
                
            return moneyCollection;
        }
       
       
        public bool Evaluate_Economy()
        {
            this.tradeEconomy.Portfolios.Evaluate_Shares_Money();
            this.Evaluate_Traders_Money();
            return true;
        }
       
        
        private double Evaluate_Traders_Money()
        {
            
            double moneyTraders = 0;
            for (int i = 0; i < this.tradeEconomy.Traders.Items.Count; i++)
            {
                
                ITrader trader = this.tradeEconomy.Traders[i];
                double moneyCash = trader.Money_Current;
                moneyTraders += moneyCash;
                if (trader.Portfolios != null && trader.Portfolios.Items.Count > 0)
                {
                    for (int j = 0; j < trader.Portfolios.Items.Count; j++)
                    {
                        double moneyPort = ((IPortfolio)(trader.Portfolios[j])).Value_Total;
                        moneyTraders += moneyPort;

                    }
                }
               

            }
            Debug.WriteLine("Money Traders = " + moneyTraders);


            return moneyTraders;
        }
     
    
     

       
      
        #endregion

        public override bool DeSerialize()
        {
            if (base.DeSerialize())
            {
                tradeEconomy = this.stockObjectBase as ITradeEconomy;

                return true;
            }

            return false;
           


        }

     
      
        //private void GetEconomyPortfolios()
        //{
        //    for (int i = 0; i < this.tradeEconomy.Portfolios.Items.Count; i++)
        //    {
        //        IPortfolio actualPortfolio = this.tradeEconomy.Portfolios[i] as IPortfolio;
        //        object o = (object)actualPortfolio;

        //        if (actualPortfolio.DeSerialize(ref o))
        //        {
        //            IPortfolio port = o as IPortfolio;
        //            System.Diagnostics.Debug.Assert(port != null, "Portfolio file not valid : " + actualPortfolio.Name);
        //            if (port != null)
        //            {
        //                tradeEconomy.Portfolios[i] = port;
        //                //have to set share data !
        //                //for (int j = 0; j < port.DataItems.Count; j++)
        //                //{
        //                //    //IDataItem di = port.DataItems[j] as IDataItem;
        //                //    //di.DataClass.Intraday = this.configuration.IntradayModus;
        //                //    //di.DataClass.Parent = di;

        //                //}
        //            }
        //        }
        //        else
        //        {
        //            System.Diagnostics.Debug.Assert(false, "Portfolio file not valid : " + actualPortfolio.Name);

        //        }
        //    }
        //}

      
        //private bool FindEconomyStatus(DateTime dateCurrent)
        //{
        //    Debug.Assert(this.tradeEconomy.TradeEconomyProfile != null);
        //    if (this.tradeEconomy.TradeEconomyProfile != null)
        //        return false;

        //    Debug.Assert(this.tradeEconomy.TradeEconomyProfile.EconomyStatusCollection != null);
        //    if (this.tradeEconomy.TradeEconomyProfile.EconomyStatusCollection != null)
        //        return false;

        //    DateTime dMin = new DateTime(1,1,1);
        //    DateTime dMax = new DateTime(5000,1,1);;
           
            
        //    for (int i = 0; i < this.tradeEconomy.TradeEconomyProfile.EconomyStatusCollection.Count; i++)
        //    {

        //        IEconomyStatus es = this.tradeEconomy.TradeEconomyProfile.EconomyStatusCollection[i] as EconomyStatus;
        //        if (dateCurrent < es.Date)
        //        {
        //            dMin = es.Date;
                    

        //        }
        //        if (dateCurrent > es.Date)
        //        {
        //            dMax = es.Date;
        //            break;
        //        }

        //    }
        //    //this.tradeEconomy.
        //    Debug.WriteLine("DateMin and DateMax are " + dMin.ToShortDateString() + " DateMax = " + dMax.ToShortDateString());

        //    return true;
        //}
    }
}
